﻿using AlphaD.Core;
using AlphaD.Core.Functions;
using AlphaD.Core.Strategies;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace AlphaD.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            LinearDiffEq2DiscontBvp problem = new LinearDiffEq2DiscontBvp
            (
                BoundaryCondition.CreateFirst(0, 0),
                BoundaryCondition.CreateFirst(3, 1),
                new double[] { 1, 2 },
                new double[] { 1, 1, 1 },
                new double[] { 1, 1, 1 },
                new double[] { 0, 1, 0 },
                new double[] { -0.2, -0.3 }
            );

            ISlarStrategy slarStrategy = new SlarStrategy();
            ILinearDiffEq2BvpStrategy bvpStrategy = new LinearDiffEq2BvpStrategy(slarStrategy);
            ILinearDiffEq2DiscontBvpStrategy discontBvpStrategy = new LinearDiffEq2DiscontBvpStrategy(bvpStrategy);
            SolutionDetails solutionDetails = new SolutionDetails();
            IEnumerable<ISegFunction<double, double>> solution = discontBvpStrategy.Resolve(problem, solutionDetails);

            IEnumerable<KeyValuePair<double, double>>[] grid = new IEnumerable<KeyValuePair<double, double>>[solution.Count()];
            for (int i = 0; i < grid.Length; i ++)
            {
                grid[i] = GetGridFunction(solution.ElementAt(i), 10);
                LineSeries series = new LineSeries()
                {
                    ItemsSource = grid[i],
                    DependentValuePath="Value",
                    IndependentValuePath="Key"                    
                };
                ColumnChart1.Series.Add(series);
            }                

            //List<KeyValuePair<string, int>> MyValue = new List<KeyValuePair<string, int>>();
            //MyValue.Add(new KeyValuePair<string, int>("Administration", 20));
            //MyValue.Add(new KeyValuePair<string, int>("Management", 36));
            //MyValue.Add(new KeyValuePair<string, int>("Development", 89));
            //MyValue.Add(new KeyValuePair<string, int>("Support", 270));
            //MyValue.Add(new KeyValuePair<string, int>("Sales", 140));

            
            //ColumnChart1.DataContext = grid[0]; 
        }

        public IEnumerable<KeyValuePair<double, double>> GetGridFunction(ISegFunction<double, double> f, int pointCount)
        {
            List<KeyValuePair<double, double>> result = new List<KeyValuePair<double,double>>();
            double x = f.Left;
            double h = (f.Right - f.Left) / pointCount;
            while(x < f.Right)
            {
                result.Add(new KeyValuePair<double, double>(x, f.Value(x)));
                x += h;
            }
            result.Add(new KeyValuePair<double, double>(f.Right, f.Value(f.Right)));

            return result;
        }
    }
}
